{include file="header.tpl"}

<div class="main dashboard">

    {foreach from=$projects item="project"}
    {assign var="currentTask" value=$project->getCurrentTask()}
    {if $currentTask}
    {assign var="state" value=$currentTask->getState()}
    <div class="item" id="p_{$project->id}">
        {if $user}        
        <a href="javascript:void(null);" class="build-button {$state.class}">
            <span id="sid_{$project->id}" class="{$state.button}"></span>
        </a>
        {/if}
        <div class="project-info">
            <h4><a href="/projects/view/{$project->name}">{$project->title}</a></h4>
            <p>{$state.text}</p>
        </div>
    </div>
	<script type="text/javascript">
	$(function(){ldelim}
		{$state.callback}({$project->id});
	{rdelim});
	</script>	
    {/if}
    {/foreach}
    
</div>
{if $user}
{literal}
<script type="text/javascript">
function buildInit( id ) {
    $('#sid_'+id).unbind('click').click(function(event) {
        var pid = event.target.id.substring(4);
        $.getJSON(
            '/default/planBuild/',
            {
                project_id: pid
            },
            function(res) {
                if(res.code!=200) {
                    alert(res.message);
                    return;
                }
                changeState( pid, res.body );				
            }
        );
    });
}
    
function stopInit( id ) {
    $('#sid_'+id).unbind('click').click(function(event) {
        var pid = event.target.id.substring(4);
        $.getJSON(
            '/default/cancelBuild/',
            {
                project_id: pid
            },
            function(res){
                if(res.code!=200) {
                    alert(res.message);
                    return;
                }
				changeState( pid, res.body );
            }
        );
    });
}

function changeState( pid, state ) {
	$('#p_'+pid+' .build-button').attr('class','').attr('class','build-button '+state.class);
	$('#sid_'+pid).attr('class','').attr('class',state.button);
	$('#p_'+pid+' .project-info p').text(state.text);
	if(state.callback != '') {
		eval( state.callback+'('+pid+');' ); 
	}
}

var states = {};
var updateInprogres = false;
function updateProjectsState() {
        if(updateInprogres == true)
            return;
        updateInprogres = true;
        $.getJSON(
            '/default/getProjectsState/',
            {},
            function(res){
                updateInprogres = false;
                if(res.code!=200) {
                    alert(res.message);
                    return;
                }
                for(i=0;i<res.body.length;i++) {                    
                    var item = res.body[i];                    
                    if(states[item.pid] != item.state) {
                        states[item.pid] = item.state;
                        $('#p_'+item.pid+' .build-button').attr('class','').attr('class','build-button '+item.class);
                        $('#sid_'+item.pid).attr('class','').attr('class',item.button);
                        $('#p_'+item.pid+' .project-info p').text(item.text);
                    }
                }
            }
        );
    }
</script>
{/literal}

<script type="text/javascript">
$(function(){ldelim}
var updateTimer = setInterval( updateProjectsState, 10000 );
{rdelim});
</script>
{/if}
{include file="footer.tpl"}